package com.shensi.adminCollect.service.station.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.adminCollect.domain.station.EnterpriseGrowUpValueSetting;
import com.shensi.adminCollect.mapper.station.EnterpriseGrowUpValueSettingMapper;
import com.shensi.adminCollect.service.station.IEnterpriseGrowUpValueSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @date 2024/1/15
 */
@Service
public class EnterpriseGrowUpValueSettingServiceImpl extends ServiceImpl<EnterpriseGrowUpValueSettingMapper, EnterpriseGrowUpValueSetting> implements IEnterpriseGrowUpValueSettingService{

    private static final Logger log = LoggerFactory.getLogger(EnterpriseGrowUpValueSettingServiceImpl.class);

    @Override
    public EnterpriseGrowUpValueSetting querySetting(){
        LoginUser user = SecurityUtils.getLoginUser();
        Long enterpriseId = user.getUser().getEnterpriseId();
        if (enterpriseId == null) {
            return new EnterpriseGrowUpValueSetting();
        }
        EnterpriseGrowUpValueSetting form = new EnterpriseGrowUpValueSetting();
        form.setEnterpriseId(enterpriseId);
        QueryWrapper<EnterpriseGrowUpValueSetting> qw = Wrappers.query(form);
        qw.last("limit 1");
        EnterpriseGrowUpValueSetting one = getOne(qw);
        if (one == null) {
            one = new EnterpriseGrowUpValueSetting();
            one.setExpireMode(1);
        }
        return one;
    }

    @Override
    public void saveSetting(EnterpriseGrowUpValueSetting setting){
        LoginUser user = SecurityUtils.getLoginUser();
        Long enterpriseId = user.getUser().getEnterpriseId();
        if (enterpriseId == null) {
            return;
        }
        EnterpriseGrowUpValueSetting saveSetting = querySetting();
        setting.setId(saveSetting.getId());
        setting.setEnterpriseId(enterpriseId);
        log.info("保存成长值设置,userId:[{}],setting:[{}]",user.getUser().getUserId(),setting);
        if (setting.getId() == null) {
            setting.setCreateBy(String.valueOf(user.getUser().getUserId()));
            setting.setCreateTime(LocalDateTime.now());
            setting.setUpdateTime(null);
            setting.setUpdateBy(null);
            save(setting);
        }else{
            setting.setUpdateBy(String.valueOf(user.getUser().getUserId()));
            setting.setUpdateTime(LocalDateTime.now());
            setting.setCreateTime(null);
            setting.setCreateBy(null);
            updateById(setting);
        }
    }

}
